package com.spn.blog.article.component.rabbitmq;

import com.rabbitmq.client.Channel;
import com.spn.blog.article.config.MQ;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class DeleteConsumer {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @RabbitListener(queues = MQ.DELETE_QUEUE)
    public void deleteQueueConsumer1(Message message, Channel channel){
        Boolean delete = redisTemplate.delete(new String(message.getBody()));
        log.info("======消费者1消费=====redis key:{},是否成功删除:{}",new String(message.getBody()),delete);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            log.info("======消费者1消费=====redis key:{}，出现异常消费失败!",new String(message.getBody()));
        }
    }

    @RabbitListener(queues = MQ.DEAD_LETTER_QUEUE)
    public void deadQueueConsumer(Message message, Channel channel){
        Boolean delete = redisTemplate.delete(new String(message.getBody()));
        log.info("======死信消费者消费=====redis key:{},是否成功删除:{}",new String(message.getBody()),delete);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            log.info("======死信消费者消费=====redis key:{}，出现异常消费失败!",new String(message.getBody()));
        }

    }
}
